<?php
namespace app\index\controller;

use think\Controller;
use think\facade\View;
use app\index\model\User;

class Demo7 extends Controller{
    public function test1(){
        /**
         * 不使用模板,直接将内容输出到页面
         */
        $content = '<h3 style="color:green">XIAONIBA欢迎你</h3>';
//        return $this->display($content);
//        return $this->view->display($content);
        return View::display($content);
    }

    public function test2(){
        /**
         * 使用视图将数据输出
         */

        //模板变量赋值: assign()
        //1.普通变量
        $this->view->assign('name','xiaoniba');
        $this->view->assign('age',33);

        //批量赋值
        $this->view->assign([
            'sex'=>'男',
            'salary'=>888
        ]);

        //2.array
        $goods=array(
            'id'=>1,
            'name'=>'手机',
            'model'=>'meta10',
            'price'=>'999'
        );
        $this->view->assign('goods',$goods);

        //3.输出对像
        $obj=new \stdClass();
        $obj->course = 'PHP';
        $obj->lecture = 'Xiaoniba';
        $this->view->assign('info',$obj);
        //4.输出常量
        define('BOSS','XIAONIBA');


        //在模板中输出数据
        //模板默认的目录位于当前模块的view目录,模板文件默认位于当前控制命名的目录中
        return $this->view->fetch();

    }

    public function test3(){

        $data = User::all();
        $this->view->assign('title','模板示例');
        $this->view->assign('data',$data);
        return $this->view->fetch();
}

    public function test4(){
        //分页
        //获取分页要调用查询类中的paginate(num)方法
        //每页显示5条
        $data = User::paginate(15);
        $this->view->assign('data',$data);
        return $this->view->fetch();
    }



}